import getApiData, { getApisData } from "@/services/cpasRequest";





export async function updateReport_setting_column(
  dbname: string,
  paramsData: any[],
) {
  paramsData.forEach((item: any) => {
    item.rowState = 'update';
    item.where = { id: item.id };
    delete item.reportDate;
    return item;
  });
  const postData = {
    apis: [
      {
        name: 'sqlApi.update',
        params: { name: `[${dbname}]..report_setting_column`, rows: paramsData }
      },
    ],
    reload: true,
    params: {}
  }
  const resApis = await getApisData(postData);
  const resUpdate = resApis.data['sqlApi.update']
  if (resApis.ok && resUpdate.ok) {
    return Promise.resolve({
      data: 'report_setting_column, 更改信息成功！',
      success: true,
    });
  }
  return Promise.resolve({
    data: resApis.data,
    success: false,
  });
}

export async function AddZBAdjustBatch(dbname: string, billID: string, paramsData: any[]) {
  const postData = {
    apis: [
      {
        name: 'sqlApi.update',
        params: { name: `[${dbname}].[dbo].[tmp_${billID}]`, rows: paramsData }
      },
    ],
    reload: true,
    params: {}
  }
  const resApis = await getApisData(postData);
  const resUpdate = resApis.data['sqlApi.update']
  if (resApis.ok && resUpdate.ok) {
    return Promise.resolve({
      data: `[${dbname}]..[tmp_${billID}], 批量增加项目成功！`,
      success: true,
    });
  }
  return Promise.resolve({
    data: resApis.data,
    success: false,
  });
}


// 获取审计调整新单据编号
export async function getReportItemsDatas(whereFrom: string, params: {
  dbname: string;
  Data_ID: string;
  userCode: string | undefined
}) {

  let requestName: any = '';
  if (whereFrom === '账表调整') {
    requestName = 'account.ZBAdujst.getNewBillNo';
  } else {
    requestName = 'account.auditAdjust.getNewBillNo';
  }

  const res = await getApiData(requestName, params);
  if (res.ok) {
    return Promise.resolve({
      data: res.data,
      success: true,
    });
  }
  return Promise.resolve({
    data: [],
    success: false,
  });
};


// 获取审计调整新单据编号
export async function itemsDatasEditXH(whereFrom: string, params: {
  dbname: string;
  billID: string;
  rowIndex: number;
  rowGuid: string;
  toIndex: number;
  toRowGuid: string;
}) {

  let requestName: any = '';
  if (whereFrom === '账表调整') {
    requestName = 'account.ZBAdjust.detail.editXH';
  } else {
    requestName = 'account.auditAdjust.detail.editXH';
  }

  const res = await getApiData(requestName, params);
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
};

// account.ZBAdjust.detail.editXH


// 获取左侧切换的数据
export async function getSubjectType(params: {
  dbname: string;
  Data_ID: string
}) {
  const res = await getApiData("account.subject.getType", params);
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}

// 获取左侧树数据
export async function getSubjectList(params: {
  dbname: string;
  Data_ID: string;
  isHasFZZ?: boolean;
}) {
  // account.subject.getDataList
  const res = await getApiData("account.subject.getDataList", params);
  if (res.ok) {
    return res.data;
  }
  return {};
}

// 删除分录
export async function detailDel(whereFrom: string, params: {
  dbname: string;
  billID: string;
  GUID: string;
}) {

  let requestName: any = '';
  if (whereFrom === '账表调整') {
    requestName = 'account.ZBAdjust.detail.del';
  } else {
    requestName = 'account.auditAdjust.detail.del';
  }
  const res = await getApiData(requestName, params);
  if (res.ok) return res.ok;
};


// 保存单据编辑
export async function ZBAdjustBill(whereFrom: string, params: {
  dbname: string,
  billID: string,
  Data_ID: string,
  billNo: string,
  TZLX: number | string,
  TZQJ: string,
  isTZ: string,
  remarks: string,
  editUser: string | undefined
  isRetrospect?: string | boolean,
  adjustType?: string | number
} & any) {

  let requestName: any = '';
  if (whereFrom === '账表调整') {
    requestName = 'account.ZBAdjust.bill.save';
  } else {
    requestName = 'account.auditAdjust.bill.save';
  }
  const res = await getApiData(requestName, params);
  if (res.ok) {
    return Promise.resolve({
      data: res.data,
      success: true,
    });
  }
  return Promise.resolve({
    data: res.data,
    success: false,
  });
};


// 重算单据
export async function reCaljustBill(params: {
  Data_ID: string,
  dbname: string,
  editUser: string,
  isRecalTZ: boolean;
}) {
  const res = await getApiData('account.reportItem.reCal', params);
  if (res.ok) {
    return Promise.resolve({
      data: res.data,
      success: true,
    });
  }
  return Promise.resolve({
    data: res.data,
    success: false,
  });
};





// 取消单据编辑
export async function ZBAdjustCancel(whereFrom: string, params: {
  dbname: string,
  billID: string,
  Data_ID: string,
  editUser: string | undefined
}) {

  let requestName: any = '';
  if (whereFrom === '账表调整') {
    requestName = 'account.ZBAdjust.bill.cancel';
  } else {
    requestName = 'account.auditAdjust.bill.cancel';
  }
  const res = await getApiData(requestName, params);
  if (res.ok) return res.ok;
};

// 清理调整分录明细编辑过程中生成的临时表
export async function AdjustingEntryClearTempTable(params: {
  dbname: string,
  billID: string,
}) {
  const res = await getApiData("account.adjustingEntry.clearTempTable", params);
  if (res.ok) return res.ok;
};

// 设置自动结转分录
export async function subjectGetJZKM(params: {
  dbname: string,
  Data_ID: string,
}) {
  const res = await getApiData("account.subject.getJZKM", params);
  if (res.ok) return res.data;
};

// 设置结转科目
export async function updateJZKM(params: {
  dbname: string,
  Data_ID: string,
  editUser: string | undefined,
  QCJZKM: string,
  QMJZKM: string,
  YQSYKM: string
}) {
  const res = await getApiData("account.subject.updateJZKM", params);
  if (res.ok) return res.ok;
};

// 获取指定科目的账龄设置
export async function getZLZone(params: {
  dbname: string,
  KMMC: string,
}) {
  const res = await getApiData("account.subject.getZLZone", params);
  if (res.ok) {
    return Promise.resolve({
      data: res.data,
      success: true,
    });
  }
  return Promise.resolve({
    data: [],
    success: false,
  });
};
// 更新账龄
export async function updateZL(whereFrom: string, params: {
  dbname: string,
  billID: string,
  GUID: string,
  M1: string | number,
  M2: string | number,
  M3: string | number,
  M4: string | number,
  M5: string | number,
  M6: string | number,
  M7: string | number,
  M8: string | number,
  M9: string | number,
  M10: string | number,
  M11: string | number,
  M12: string | number,
  Y1: string | number,
  Y2: string | number,
  Y3: string | number,
  Y4: string | number,
  Y5: string | number,
  Y6: string | number,
  Y7: string | number,
  Y8: string | number,
  Y9: string | number,
  Y10: string | number
}) {
  let requestName: any = '';
  if (whereFrom === '账表调整') {
    requestName = 'account.ZBAdjust.detail.updateZL';
  } else {
    requestName = 'account.auditAdjust.detail.updateZL';
  };
  const res = await getApiData(requestName, params);
  if (res.ok) return res.ok;
};




export async function ZBAdjustEditHCFSE(params: {
  dbname: string,
  billID: string,
  isEditHCFSE: boolean;
  GUID: string,
  isHCFSE: string,
}) {
  const res = await getApiData('account.ZBAdjust.detail.edit', params);
  if (res.ok) {
    return Promise.resolve({
      data: res.data,
      success: true,
    });
  }
  return Promise.resolve({
    data: res.data,
    success: false,
  });
}



